package com.example.jpa.controller;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;

import java.io.File;
import java.io.FileOutputStream;

import static com.example.jpa.controller.JsonToXml.jsonToXml;

/**
 * @author 朝花不迟暮
 * @version 1.0
 * @date 2020/12/12 19:51
 */
public class Test
{
    public static void main(String[] args) throws Exception
    {
        final String STR_JSON = "{\"name\":\"yato233\",\"address\":{\"city\":\"beijing\",\"street\":\" longyu Road \",\"postcode\":100000},\"blog\":\"https://blog.csdn.net/baidu_38322198\"}";
        Document document = DocumentHelper.createDocument();
        Element root = document.addElement("stakeapply"); //默认根节点
        Element el = jsonToXml(STR_JSON, root);
        System.out.println(el.asXML());
        try
        {
            //生成xml文件
            String fileName = "test.xml";
            OutputFormat format = OutputFormat.createPrettyPrint();
            format.setEncoding("UTF-8"); // 指定XML编码
            format.setExpandEmptyElements(true);//自动添加闭合标签
            document.setXMLEncoding("UTF-8");
            //指定文件路径，名字，格式
            XMLWriter writer = new XMLWriter(
                    new FileOutputStream(new File("D:\\xml" + File.separator + fileName)), format);
            writer.write(document);
            writer.close();
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }
}
